/*
	DS Kanji Flash
	Copyright (C) 2009,2010 Bernhard Schelling

	This file is part of DS Kanji Flash.

	DS Kanji Flash is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	DS Kanji Flash is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with DS Kanji Flash.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "global.h"
#include "dict.h"

#define SCN_RECORD 4 //unused so define it here

static sRect rbtnBack(20, 12, 216, 8);
static wchar_t wcChar;
static UL_IMAGE *imgKana = NULL;

class cSceneRecord : public cScene
{
public:
	cSceneRecord() : cScene(SCN_RECORD)
		{ WantsKeyHeldEvents = true; WantsKeyReleaseEvents = true; }
	
	void LoadKanaImg()
	{
		ulSetLoadUtilitySource(VF_MEMORY);
		if (imgKana) ulDeleteImage(imgKana);
		if ((kanapool_png[wcChar-0x3000+1]-kanapool_png[wcChar-0x3000]) > 0)
		{
			imgKana = ulLoadImageFilePNG((char*)kanapool_png[wcChar-0x3000], (int)(kanapool_png[wcChar-0x3000+1]-kanapool_png[wcChar-0x3000]), UL_IN_VRAM, UL_PF_PAL4);
			UL_COLOR* pal = (UL_COLOR*)ulLockImage(imgKana, UL_LOCK_PALETTE);
			for (int i=0;i<16;i++) pal[i] = RGB15(31-(31-(pal[i]>>10))/2, 31-(31-(pal[i]>>10))/2, 31-(31-(pal[i]>>10))/2);
			ulUnlockImage(imgKana, UL_LOCK_PALETTE);
		}
	}

	virtual void Enter(void *p1, void *p2, void *p3)
	{
		wcChar = 0x3041;
		//wcChar = 0x30A1;
		LoadKanaImg();
		DrawClearImage(true);
	}

	virtual void ProcessInput()
	{
		if (ul_keys.pressed.touch && rbtnBack.touchButton()) cScene::GoTo(SCN_CARD);
		DrawProcessInput();
		if (ul_keys.pressed.L || ul_keys.pressed.R)
		{
			FILE *pfRecord = NULL;
			if (DrawOutDrawnPosString(NULL) && (pfRecord = fopen("record.txt", "a")))
			{
				char pcChar[4]; pcChar[SetUCS(pcChar, wcChar)] = 0;
				fputs(pcChar, pfRecord);
				DrawOutDrawnPosString(pfRecord);
				fputs("\n", pfRecord);
				fclose(pfRecord);
			}
		}
		if (ul_keys.pressed.R) { wcChar++; LoadKanaImg(); }
		if (ul_keys.pressed.L) { wcChar--; LoadKanaImg(); }
		if (ul_keys.pressed.L || ul_keys.pressed.R || ul_keys.pressed.select) DrawClearImage(true);
	}

	virtual void DrawScreen_Top()
	{
		ulDrawGradientRect(0, 0, 256, 192, RGB15(31, 31, 31), RGB15(31, 31, 31), RGB15(25, 25, 25), RGB15(25, 25, 25));
		int dx = (256-128-10), dy = (192-128)/2;
		ulDrawFillRect(dx-2, dy-2, dx+42+2, dy+42+2, COL_BLACK);
		ulDrawFillRect(dx-0, dy-0, dx+42+0, dy+42+0, COL_WHITE);
		ulScaleScreenView(inttof32(3), inttof32(3));
		char pcChar[4]; pcChar[SetUCS(pcChar, wcChar)] = 0; displayString(fntJapanese, pcChar, dx/3+2, dy/3);
		ulResetScreenView();
	}

	virtual void DrawScreen_Bottom()
	{
		ulDrawGradientRect(0, 0, 256, 192, RGB15(31, 31, 31), RGB15(31, 31, 31), RGB15(25, 25, 25), RGB15(25, 25, 25));

		if (imgKana) imgKana->stretchX = imgKana->stretchY = 128;
		DrawDisplay(NULL, imgKana);

		drawButtonBG(rbtnBack, BUTTON_BG_BLUE, "Back");
	}
}; //unused: SceneRecord;
